package sk.fritz.megafonsms.formmodel;

import java.util.Hashtable;

import sk.fritz.megafonsms.formparser.FormListener;

public class Builder implements FormListener {
	
	private Hashtable models = new Hashtable();
	
	private FormModel current;
		
	public FormModel getModel(String id) {
		return (FormModel) models.get(id);
	}

	public void clear() {
		models.clear();
	}

	public void form(String id, String action, String method, String enctype) {
		current = new FormModel();
		current.setFormAction(action);
		current.setFormMethod(method);
		current.setEncType(enctype);
		models.put(id, current);
	}

	public void input(String type, String name, String value, Integer maxLen) {
		InputFormControl ctrl = new InputFormControl();
		ctrl.setName(name);
		ctrl.setValue(value);
		ctrl.setMaxLength(maxLen);
		ctrl.setType(InputFormControl.parseType(type));
		current.addFormControl(ctrl);
	}

	public void select(String name, Option[] options, int selected) {
		SelectFormControl slct = new SelectFormControl();
		slct.setName(name);
		for (int i = 0; i < options.length; i++) {
			slct.addOption(options[i]);
		}
		slct.setSelectedIndx(selected);
		current.addFormControl(slct);
	}

}
